﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Business
{
    public class HoaDonBan
    {
        
        public int MaHDB { get; set; }
        public string MaHDBHienThi
        {
            get
            {
                return "HDB" + MaHDB;
            }
        }
        public int MaNV { get; set; }
        public string MaNVHienThi
        {
            get
            {
                return "NV" + MaNV;
            }
        }
        public int MaDDH { get; set; }
        public string MaDDHHienThi
        {
            get
            {
                return "DDKH" + MaDDH;
            }
        }
        public DateTime NgayXuatHD { get; set; }
        public double TienTruocThue { get; set; }
        public double TienSauThue { get; set; }

        public double ThueSuat { get; set; }

        public double ThueSuatHienThi
        {
            get
            {
                return ThueSuat * 100;
            }
        }
        public List<ChiTietHDBan> DanhSachChiTietHDBan { get; set; }
        

      
        public HoaDonBan()
        {
            this.MaHDB = 0;
            this.ThueSuat = 0.1;
            DanhSachChiTietHDBan = new List<ChiTietHDBan>();
        }

        public HoaDonBan(int maHDB, int maNV, int maDDH, DateTime ngayXuatHD,
            double tienTruocThue, double tienSauThue, double thueSuat)
        {
            this.MaHDB = maHDB;
            this.MaNV = maNV;
            this.MaDDH = maDDH;
            this.NgayXuatHD = ngayXuatHD;
            this.TienTruocThue = tienTruocThue;
            this.TienSauThue = tienSauThue;
            this.ThueSuat = thueSuat;
        }

        
        public void ThemMoiChiTietHDBan(int maHDB, int maHang,
            string tenHang, int soLuong, double donGiaBan, double chietKhau)
        {
            ChiTietHDBan chiTietHDB = new ChiTietHDBan(maHDB, maHang, tenHang, soLuong, donGiaBan, chietKhau);

            DanhSachChiTietHDBan.Add(chiTietHDB);
        }

        public void CapNhatChiTietDonDat(int index, int maHang,
            string tenHang, int soLuong, double donGiaBan, double chietKhau)
        {
            ChiTietHDBan chiTietDonDat = DanhSachChiTietHDBan[index];
            chiTietDonDat.CapNhat(maHang, tenHang, soLuong, donGiaBan, chietKhau);
        }

        public void XoaChiTietHDBan(int index)
        {
            DanhSachChiTietHDBan.RemoveAt(index);
        }

        private bool TonTaiChiTietHDBan(int maCTHDB)
        {
            foreach (var chitietHDBan in this.DanhSachChiTietHDBan)
            {
                if (chitietHDBan.MaCTHDB == maCTHDB)
                    return true;
            }
            return false;
        }

        private double TinhTongTienTruocThue()
        {
            double tongTien = 0;
            foreach (var chiTietHDB in DanhSachChiTietHDBan)
            {
                double giaTriThuc = chiTietHDB.DonGiaBan - chiTietHDB.DonGiaBan * chiTietHDB.ChietKhau;
                tongTien = tongTien + chiTietHDB.SoLuong * giaTriThuc;
            }
            this.TienTruocThue = tongTien;
            return tongTien;
        }


        public static int ThemMoi(HoaDonBan nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            DataAccess.HoaDonBan tamObject = new DataAccess.HoaDonBan();
            tamObject.MaDDH = nhapObject.MaDDH;
            tamObject.MaNV = nhapObject.MaNV;

            tamObject.NgayXuatHD = nhapObject.NgayXuatHD;

            tamObject.ThueSuat = nhapObject.ThueSuat;
            tamObject.TienTruocThue = nhapObject.TienTruocThue;

            tamObject.TienSauThue = nhapObject.TienSauThue;
            db.HoaDonBans.InsertOnSubmit(tamObject);

            db.SubmitChanges();

            foreach (var chitietHDBan in nhapObject.DanhSachChiTietHDBan)
            {
                DataAccess.ChiTietHDBan chiTietTamObject = new DataAccess.ChiTietHDBan();
                chiTietTamObject.MaHDB = tamObject.MaHDB;
                chiTietTamObject.MaHang = chitietHDBan.MaHang;

                chiTietTamObject.SoLuong = chitietHDBan.SoLuong;
                chiTietTamObject.DonGiaBan = chitietHDBan.DonGiaBan;

                chiTietTamObject.ChietKhau = chitietHDBan.ChietKhau;

                db.ChiTietHDBans.InsertOnSubmit(chiTietTamObject);
            }

            db.SubmitChanges();

            return tamObject.MaHDB;
        }

        public double TinhTongTienSauThue(double thueSuatPhanTram)
        {
            this.ThueSuat = thueSuatPhanTram / 100;
            double tienTruocThue = TinhTongTienTruocThue();
            double tienSauThue = tienTruocThue + tienTruocThue * this.ThueSuat;
            this.TienSauThue = tienSauThue;
            return tienSauThue;
        }

        public static bool ChoPhepXoaChinhSua(int maHDB)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            int hoaDonBan_PXK = (from p in db.PhieuXuatKhos
                                 where p.MaHDB == maHDB
                                 select p).Count();
            int hoaDonBan_PGH = (from p in db.PhieuGiaoHangs
                                 where p.MaHDB == maHDB
                                 select p).Count();

            if(hoaDonBan_PXK > 0 || hoaDonBan_PGH > 0)
                return false;
            else
                return true;
        }
        

        

        public static void CapNhat(HoaDonBan nhapObject)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            
            var tamObject = (from p in db.HoaDonBans
                             where p.MaHDB == nhapObject.MaHDB
                             select p).Single();

            tamObject.MaDDH = nhapObject.MaDDH;
            tamObject.MaNV = nhapObject.MaNV;
            tamObject.NgayXuatHD = nhapObject.NgayXuatHD;

            tamObject.ThueSuat = nhapObject.ThueSuat;
            tamObject.TienTruocThue = nhapObject.TienTruocThue;

            tamObject.TienSauThue = nhapObject.TienSauThue;

            
            foreach (var chiTietTamObject in tamObject.ChiTietHDBans)
            {
                if (!nhapObject.TonTaiChiTietHDBan(chiTietTamObject.MaCTHDB))
                    db.ChiTietHDBans.DeleteOnSubmit(chiTietTamObject);
            }

            
            foreach (var chiTietHDBan in nhapObject.DanhSachChiTietHDBan)
            {
                if (chiTietHDBan.MaCTHDB == 0)
                {
                    DataAccess.ChiTietHDBan chiTietTamObject = new DataAccess.ChiTietHDBan();
                    chiTietTamObject.MaHDB = tamObject.MaHDB;

                    chiTietTamObject.MaHang = chiTietHDBan.MaHang;
                    chiTietTamObject.SoLuong = chiTietHDBan.SoLuong;

                    chiTietTamObject.DonGiaBan = chiTietHDBan.DonGiaBan;
                    chiTietTamObject.ChietKhau = chiTietHDBan.ChietKhau;

                    db.ChiTietHDBans.InsertOnSubmit(chiTietTamObject);
                }
                else
                {
                    var chiTietTamObject = (from p in db.ChiTietHDBans
                                            where p.MaCTHDB == chiTietHDBan.MaCTHDB
                                            select p).Single();

                    chiTietTamObject.MaHDB = tamObject.MaHDB;

                    chiTietTamObject.MaHang = chiTietHDBan.MaHang;
                    chiTietTamObject.SoLuong = chiTietHDBan.SoLuong;
                    chiTietTamObject.DonGiaBan = chiTietHDBan.DonGiaBan;
                    chiTietTamObject.ChietKhau = chiTietHDBan.ChietKhau;
                }
            }

            db.SubmitChanges();
        }

        public static void Xoa(int maHDBan)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

            var xoaObject = (from p in db.HoaDonBans
                             where p.MaHDB == maHDBan
                             select p).Single();
            db.HoaDonBans.DeleteOnSubmit(xoaObject);

            db.SubmitChanges();
        }

        public static HoaDonBan LayHoaDonBan(int maHDB)
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            HoaDonBan hoaDonBan = (from p in db.HoaDonBans
                                   where p.MaHDB == maHDB
                                   select new HoaDonBan(p.MaHDB, p.MaNV, p.MaDDH, p.NgayXuatHD, p.TienTruocThue, p.TienSauThue, p.ThueSuat)).Single();

            List<ChiTietHDBan> danhsachChiTietDonDat = (from p in db.ChiTietHDBans
                                                        where p.MaHDB == maHDB
                                                        select new ChiTietHDBan(p.MaCTHDB, p.MaHDB, p.MaHang, p.HangHoa.TenHang, p.SoLuong, p.DonGiaBan, p.ChietKhau)).ToList();
            if (danhsachChiTietDonDat == null || danhsachChiTietDonDat.Count == 0)
                danhsachChiTietDonDat = new List<ChiTietHDBan>();

            hoaDonBan.DanhSachChiTietHDBan = danhsachChiTietDonDat;

            return hoaDonBan;
        }

        public static List<HoaDonBan> LayDanhSach()
        {
            DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
            return (from p in db.HoaDonBans
                    select new HoaDonBan(p.MaHDB, p.MaNV, p.MaDDH, p.NgayXuatHD, p.TienTruocThue, p.TienSauThue, p.ThueSuat)).ToList();
        }

        public static List<HoaDonBan> LayDanhSach_NgayMua(DateTime tungay, DateTime denngay)
        {
            try
            {
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.HoaDonBans
                        where p.NgayXuatHD.Date >= tungay.Date && p.NgayXuatHD.Date <= denngay.Date
                        select new HoaDonBan(p.MaHDB, p.MaNV, p.MaDDH, p.NgayXuatHD, p.TienTruocThue, p.TienSauThue, p.ThueSuat)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }

        public static List<HoaDonBan> LayDanhSach_MaDonDat(string maDDHT)
        {

            try
            {

                int maDDH = Int16.Parse(maDDHT.Substring(4));
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();

                return (from p in db.HoaDonBans
                        where p.MaDDH == maDDH

                        select new HoaDonBan(p.MaHDB, p.MaNV, p.MaDDH, p.NgayXuatHD, p.TienTruocThue, p.TienSauThue, p.ThueSuat)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }

        public static List<HoaDonBan> LayDanhSach_MaHoaDonBan(string maHDBHienThi)
        {
            try
            {
                int maHDB = Int16.Parse(maHDBHienThi.Substring(3));
                DataAccess.KhoBanHangDataContext db = new DataAccess.KhoBanHangDataContext();
                return (from p in db.HoaDonBans

                        where p.MaHDB == maHDB
                        select new HoaDonBan(p.MaHDB, p.MaNV, p.MaDDH, p.NgayXuatHD, p.TienTruocThue, p.TienSauThue, p.ThueSuat)).ToList();
            }
            catch
            {
                return LayDanhSach();
            }
        }
        
    }
}
